using System;
using System.Collections.Generic;
using System.Text;

using IGlobalist.Cms.Model;
using IGlobalist.Cms.DataAccess;

namespace IGlobalist.Cms.Bll
{
    public class ResourceFileController : BaseController
    {
        private static IResourceFile ConvertCmsObject2ResourceFile(ICmsObject obj)
        {
            IResourceFile resourceFile = new ResourceFile();
            resourceFile.FromCmsObject(obj);
            return resourceFile;
        }


        private static IList<IResourceFile> ConvertListObject2ListResourceFiles(IList<ICmsObject> list)
        {
            IList<IResourceFile> resourceFiles = new List<IResourceFile>();
            if (list != null)
            foreach (ICmsObject obj in list)
            {
                resourceFiles.Add(ConvertCmsObject2ResourceFile(obj));
            }

            return resourceFiles;
        }


        public static IList<IResourceFile> GetResourceFileListByFolderID(long FolderID)
        {
            IList<IResourceFile> resourceList = new List<IResourceFile>();
            ICmsObject obj = _Ds.GetObject(FolderID);
            if(obj != null)
            {
                foreach (ICmsObject cmsObject in obj.SubObjectList)
                {
                    if (cmsObject.ClassTypeID == (short)CmsObjectType.ResourceFile)
                    {
                        IResourceFile resourceFile = new ResourceFile();
                        resourceFile.FromCmsObject(cmsObject);
                        resourceList.Add(resourceFile);
                    }
                }               
            }
            return resourceList;
        }

        public static IResourceFile GetResourceFile(long id)
        {
            return ConvertCmsObject2ResourceFile(_Ds.GetObject(id));
        }


        public static IList<IResourceFile> GetResourceFileList(long classID)
        {
            return ConvertListObject2ListResourceFiles(_Ds.GetObjectListByClassID(classID));
        }

        public static IList<IResourceFile> GetResourceFileList(ResourceType resourceType)
        {
            IList<IResourceFile> resources = ConvertListObject2ListResourceFiles(_Ds.GetObjectListAllByClassTypeID((short) CmsObjectType.ResourceFile));
            int n=0;
            while (n<resources.Count ) {
                if (!resources[n].Type.Equals( resourceType))
                {
                    resources.RemoveAt(n);
                }else{
                    n++;
                }           
            }
            return resources;
        }



        public static long InsertResourceFile(IResourceFile resourceFile)
        {
            long id = _Ds.InsertObject((ICmsObject)resourceFile);
            if (resourceFile.FolderID > 0)
            {
                _Ds.InsertObjectRelation(resourceFile.FolderID, id, (short)CmsObjectType.ResourceFolder);
            }
            return id;
        }

        public static void UpdateResourceFile(IResourceFile resourceFile)
        {
            _Ds.UpdateObject((ICmsObject)resourceFile);

            IResourceFile resourceFileOld = GetResourceFile(resourceFile.ID);
            if (resourceFileOld.FolderID != resourceFile.FolderID)
            {
                _Ds.DeleteObjectOneRelation(resourceFileOld.FolderID, resourceFile.ID);
                if (resourceFile.FolderID > 0)
                {
                    _Ds.InsertObjectRelation(resourceFile.FolderID, resourceFile.ID, (short)CmsObjectType.ResourceFolder);
                }
            }
        }

        public static void DeleteResourceFile(long id)
        {
            IResourceFile rs = ResourceFileController.GetResourceFile(id);
            _Ds.DeleteObject(id);
            _Ds.ClearCacheObject(rs.FolderID);
           
        }
    }
}
